#include <iostream>
#include <sstream>
#include <thread>

#include "proj/ThreadPool.h"

class MyTask final : public zdotp::TaskBase {
    int begin_;
    int end_;
public:
    explicit MyTask(const int begin, const int end) : begin_(begin), end_(end) {}
    zdotp::Any run() override
    {
        int sum = 0;
        for (int i = begin_; i <= end_; ++ i) {
            sum += i;
        }

        std::this_thread::sleep_for(std::chrono::seconds(1));

        std::ostringstream oss;
        oss << std::this_thread::get_id();
        printf("Calc %d to %d, on Thread %s\n", begin_, end_, oss.str().c_str());

        return sum;
    }
};
int main()
{
    {
        zdotp::ThreadPool pool;
        pool.set_mode(zdotp::thread_pool_mode::MODE_CACHED);
        pool.start(3);
        auto result1 = pool.submit_task(std::make_shared<MyTask>(1, 100));
        auto result2 = pool.submit_task(std::make_shared<MyTask>(101, 200));
        auto result3 = pool.submit_task(std::make_shared<MyTask>(201, 300));
        auto result4 = pool.submit_task(std::make_shared<MyTask>(301, 400));

        // ((1 + 400) * 400 / 2) = 80200
        auto answer =
            result1.get().cast<int>() +
                result2.get().cast<int>() +
                    result3.get().cast<int>() +
                        result4.get().cast<int>();
        printf("answer : %d\n", answer);
    }

    getchar();
    return 0;
}
